package com.qf.smsplatform.strategy.impl;

import com.qf.smsplatform.common.constants.StrategyConstants;
import com.qf.smsplatform.common.model.StandardSubmit;
import com.qf.smsplatform.dfa.SensitivewordFilter;
import com.qf.smsplatform.strategy.Strategy;
import com.qf.smsplatform.util.PushMessage2Queue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

@Service(value = "dirtyWord")
@Slf4j
public class DirtyWordStrategy implements Strategy {

    @Autowired
    private PushMessage2Queue pushMessage2Queue;

    @Override
    public boolean strategy(StandardSubmit submit) {
        log.info("【策略模块-敏感词校验】 敏感词校验策略开始执行！！");

        //1. 获取校验敏感词的过滤器
        SensitivewordFilter filter = SensitivewordFilter.getInstance();

        //2. 校验敏感词
        int count = filter.checkSensitiveWord(submit.getMessageContent(), 0, SensitivewordFilter.minMatchType);
        Set<String> sensitiveWord = filter.getSensitiveWord(submit.getMessageContent(), SensitivewordFilter.minMatchType);
        //3. 判断
        if(count > 0){
            // 有敏感词
            log.info("【策略模块-敏感词校验】 短信内容中存在敏感词, sensitiveWord = {}",sensitiveWord);
            // 下发日志TOPIC(写日志到ES),
            pushMessage2Queue.sendLogMessage(submit, StrategyConstants.STRATEGY_ERROR_DIRTY_WORDS);
            // 状态报告TOPIC(接口模块通知客户端)
            pushMessage2Queue.pushReportMessage(submit,StrategyConstants.STRATEGY_ERROR_DIRTY_WORDS);
            return false;
        }
        log.info("【策略模块-敏感词校验】 敏感词校验策略执行结束！！");
        return true;
    }
}
